package com.tuishan.myapplication.Views.Fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.tuishan.myapplication.Base.BaseContest;
import com.tuishan.myapplication.MainActivity;
import com.tuishan.myapplication.Model.UserModel;
import com.tuishan.myapplication.MyApplication;
import com.tuishan.myapplication.R;
import com.tuishan.myapplication.Servers.BaseCallback;
import com.tuishan.myapplication.Servers.OkHttpHelper;
import com.tuishan.myapplication.Servers.UrlRouter;
import com.tuishan.myapplication.Views.Login.LoginActivity;
import com.tuishan.myapplication.Views.Users.SetEmailActivity;
import com.tuishan.myapplication.Views.Users.SetPassWordActivity;
import com.tuishan.myapplication.Views.Users.SetUserInfoActivity;
import com.tuishan.myapplication.Views.Users.UserCollectionActivity;
import com.tuishan.myapplication.Views.Users.UserLogActivity;

import okhttp3.Request;
import okhttp3.Response;

/**
 * <pre>
 *     time   : 2021/04/14
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class Fragment_User extends Fragment implements View.OnClickListener {
    LinearLayout UserItem_Ly, UserItem_Email_Ly, Trans_PW_Item_Ly,Collection_Item_Ly;
    Button LoginOut_btn;
    ImageButton Identity_btn, SafeSet_btn;
    TextView user_email_tx, user_school_tx, user_money_tx;
    ImageView user_sex_image;

    MyApplication myApp;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user_layout, container, false);
        UserItem_Ly = view.findViewById(R.id.UserItem_Ly);
        UserItem_Email_Ly = view.findViewById(R.id.UserItem_Email_Ly);
        LoginOut_btn = view.findViewById(R.id.LoginOut_btn);
        Identity_btn = view.findViewById(R.id.Identity_btn);
        SafeSet_btn = view.findViewById(R.id.SafeSet_btn);
        Trans_PW_Item_Ly = view.findViewById(R.id.Trans_PW_Item_Ly);
        Collection_Item_Ly = view.findViewById(R.id.Collection_Item_Ly);

        user_email_tx = view.findViewById(R.id.user_email_tx);
        user_school_tx = view.findViewById(R.id.user_school_tx);
        user_sex_image = view.findViewById(R.id.user_sex_image);
        user_money_tx = view.findViewById(R.id.user_money_tx);
        UserItem_Ly.setOnClickListener(this);
        Collection_Item_Ly.setOnClickListener(this);
        Trans_PW_Item_Ly.setOnClickListener(this);
        UserItem_Email_Ly.setOnClickListener(this);
        LoginOut_btn.setOnClickListener(this);
        Identity_btn.setOnClickListener(this);
        SafeSet_btn.setOnClickListener(this);
        myApp = (MyApplication) getActivity().getApplication();
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        GetUserInfo();
        if (!myApp.getLogin()) {
            OutLogin();
        }
    }

    private void GetUserInfo() {
        OkHttpHelper mHttpHelper = OkHttpHelper.getInstance();
        mHttpHelper.get(UrlRouter.UserInfo, UserModel.class, new BaseCallback<UserModel>() {
            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onSuccess(Response response, UserModel o) {
                user_email_tx.setText(o.getNickName() == null ? o.getEmail() : o.getNickName());
                user_school_tx.setText(o.getSchool());
                switch (o.getSex()) {
                    case 1:
                        user_sex_image.setImageDrawable(getResources().getDrawable(R.drawable.user_boys_icon));
                        break;
                    case 2:
                        user_sex_image.setImageDrawable(getResources().getDrawable(R.drawable.user_girls_icon));
                        break;
                }
                user_money_tx.setText("余额：" + o.getMoney());
            }

            @Override
            public void onFalse(Response response, String err_msg) {

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.Trans_PW_Item_Ly:
                Intent intentpas = new Intent(getActivity(), SetPassWordActivity.class);
                intentpas.putExtra("type",9);
                startActivity(intentpas);
                break;
            case R.id.UserItem_Email_Ly:
                Intent intentEm = new Intent(getActivity(), SetEmailActivity.class);
                startActivity(intentEm);
                break;
            case R.id.UserItem_Ly:
                Intent intentLog = new Intent(getActivity(), UserLogActivity.class);
                startActivity(intentLog);
                break;
            case R.id.Collection_Item_Ly:
                Intent intentColl = new Intent(getActivity(), UserCollectionActivity.class);
                startActivity(intentColl);
                break;
            case R.id.LoginOut_btn:
                OutLogin();
                break;
            case R.id.Identity_btn:
                Intent intentIdent = new Intent(getActivity(), SetUserInfoActivity.class);
                startActivity(intentIdent);
                break;
            case R.id.SafeSet_btn:
                Intent intentpa = new Intent(getActivity(), SetPassWordActivity.class);
                intentpa .putExtra("type",7);
                startActivity(intentpa);
                break;
        }
    }

    void OutLogin() {
        myApp.setLogin(false);
        getActivity().finish();
        SPUtils.getInstance().remove(BaseContest.UserToken);
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
    }

}
